<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据查询工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .config-section {
            margin-bottom: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        .section-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            color: #333;
            border-bottom: 1px solid #ddd;
            padding-bottom: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 120px;
            font-weight: bold;
        }
        input[type="text"], input[type="password"], select {
            width: 250px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            padding: 8px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
        .test-btn {
            background-color: #2196F3;
        }
        .test-btn:hover {
            background-color: #0b7dda;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .search-section {
            margin-top: 30px;
        }
        .search-conditions {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 15px;
        }
        .search-condition {
            flex: 1;
            min-width: 200px;
        }
        .action-buttons {
            margin-top: 15px;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>数据查询工具</h1>

    <!-- 数据库配置部分 -->
    <div class="config-section">
        <div class="section-title">数据库配置</div>
        <div class="form-group">
            <label for="dbHost">服务器地址:</label>
            <input type="text" id="dbHost" placeholder="例如: localhost">
        </div>
        <div class="form-group">
            <label for="dbPort">端口号:</label>
            <input type="text" id="dbPort" placeholder="例如: 1433">
        </div>
        <div class="form-group">
            <label for="dbName">数据库名:</label>
            <input type="text" id="dbName" placeholder="数据库名称">
        </div>
        <div class="form-group">
            <label for="dbUser">用户名:</label>
            <input type="text" id="dbUser" placeholder="用户名">
        </div>
        <div class="form-group">
            <label for="dbPassword">密码:</label>
            <input type="password" id="dbPassword" placeholder="密码">
        </div>
        <button class="test-btn" onclick="testDbConnection()">测试连接</button>
    </div>

    <!-- 共享文件夹配置部分 -->
    <div class="config-section">
        <div class="section-title">Windows共享文件夹配置</div>
        <div class="form-group">
            <label for="sharePath">服务器地址:</label>
            <input type="text" id="sharePath" placeholder="例如: 192.168.168.115">
        </div>
        <div class="form-group">
            <label for="shareUser">用户名:</label>
            <input type="text" id="shareUser" placeholder="用户名">
        </div>
        <div class="form-group">
            <label for="sharePassword">密码:</label>
            <input type="password" id="sharePassword" placeholder="密码">
        </div>
        <button class="test-btn" onclick="testShareConnection()">测试连接</button>
    </div>

    <!-- 搜索条件部分 -->
    <div class="search-section">
        <div class="section-title">搜索条件</div>
        <div class="search-conditions">
            <div class="search-condition">
                <label for="partNo">零件编号like:</label>
                <input type="text" id="partNo" placeholder="">
            </div>
            <div class="search-condition">
                <label for="machineName">基准机床名称like:</label>
                <input type="text" id="machineName" placeholder="">
            </div>
            <div class="search-condition">
                <label for="materialCode">材料代码like:</label>
                <input type="text" id="materialCode" placeholder="">
            </div>
            <div class="search-condition">
                <label for="startUpdateTime">最近更新日期>=:</label>
                <input type="text" id="startUpdateTime" placeholder="">
            </div>
            <div class="search-condition">
                <label for="endUpdateTime">最近更新日期<=:</label>
                <input type="text" id="endUpdateTime" placeholder="">
            </div>
        </div>
        <div class="action-buttons">
            <button onclick="search()">搜索</button>
            <button onclick="exportData()">导出Excel</button>
            <button onclick="clearConditions()">清空条件</button>
        </div>
    </div>

    <!-- 结果表格部分 -->
    <div class="results-section">
        <div class="section-title">搜索结果</div>
        <table id="resultTable">
            <thead>
            <tr>
                <th>序号</th>
                <th>字段1</th>
                <th>字段2</th>
                <th>字段3</th>
                <th>字段4</th>
                <th>字段5</th>
            </tr>
            </thead>
            <tbody>
            <!-- 示例数据 -->
            <tr>
                <td>1</td>
                <td>示例数据1</td>
                <td>示例数据2</td>
                <td>示例数据3</td>
                <td>示例数据4</td>
                <td>示例数据5</td>
            </tr>
            <tr>
                <td>2</td>
                <td>示例数据1</td>
                <td>示例数据2</td>
                <td>示例数据3</td>
                <td>示例数据4</td>
                <td>示例数据5</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
    function testDbConnection() {
        // 获取数据库配置值
        const ip = document.getElementById('dbHost').value;
        const port = document.getElementById('dbPort').value;
        const database = document.getElementById('dbName').value;
        const username = document.getElementById('dbUser').value;
        const password = document.getElementById('dbPassword').value;

        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://127.0.0.1/configDatabase');
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onload = function() {
            if (xhr.status === 200 || xhr.status === 201) {
                const data = JSON.parse(xhr.responseText);
                if (data.data){
                    alert("数据库连接成功！");
                }else {
                    alert("数据库连接失败！");
                }
            } else {
                console.error('Error:', xhr.statusText);
                alert("数据库连接失败！");
            }
        };
        xhr.send(JSON.stringify({ "ip": ip,"port": port,"database": database,"username": username,"password": password }));
    }

    function testShareConnection() {
        // 获取共享文件夹配置值
        const ip = document.getElementById('sharePath').value;
        const username = document.getElementById('shareUser').value;
        const password = document.getElementById('sharePassword').value;

        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://127.0.0.1/configShareDir');
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onload = function() {
            if (xhr.status === 200 || xhr.status === 201) {
                const data = JSON.parse(xhr.responseText);
                if (data.data){
                    alert("共享文件夹连接成功！");
                }else {
                    alert("共享文件夹连接失败！");
                }
            } else {
                console.error('Error:', xhr.statusText);
                alert("共享文件夹连接失败！");
            }
        };
        xhr.send(JSON.stringify({ "ip": ip,"username": username,"password": password }));
    }

    function search() {
        // 获取搜索条件
        const partNo = document.getElementById('partNo').value;
        const machineName = document.getElementById('machineName').value;
        const materialCode = document.getElementById('materialCode').value;
        const startUpdateTime = document.getElementById('startUpdateTime').value;
        const endUpdateTime = document.getElementById('endUpdateTime').value;
        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://127.0.0.1/search');
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onload = function() {
            if (xhr.status === 200 || xhr.status === 201) {
                const data = JSON.parse(xhr.responseText);

            } else {
                console.error('Error:', xhr.statusText);
                alert("共享文件夹连接失败！");
            }
        };
        xhr.send(JSON.stringify({ "partNo": partNo,"machineName": machineName,"materialCode": materialCode,"startUpdateTime": startUpdateTime ,"endUpdateTime": endUpdateTime  }));
    }

    function exportData() {
        // 这里应该发送AJAX请求到后端导出数据
        alert("正在导出Excel文件...");

        // 模拟异步请求
        setTimeout(() => {
            alert("导出成功！");
            // 实际应用中这里应该触发文件下载
        }, 1500);
    }

    function clearConditions() {
        // 清空所有搜索条件
        document.getElementById('condition1').value = '';
        document.getElementById('condition2').value = '';
        document.getElementById('condition3').value = '';
        document.getElementById('condition4').value = '';
        document.getElementById('condition5').value = '';
    }
</script>
</body>
</html>
